package com.flash.ticket.controller;

import com.aliyun.oss.OSS;
import com.flash.ticket.utils.SendMailUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.Date;
import java.util.Map;

/**
* @description: TODO
* @author admin
* @date 2023/1/1719:13
* @version 1.0
*/
@RequestMapping("ticket")
@RestController
public class TicketController {

    @Value("${oss.bucket}")
    private String bucketName;

    @Value("${oss.hostName}")
    private String hostName;

    @Autowired
    private OSS ossClient;

    /*用户提交公司名称、税号、邮箱、金额等信息，模拟调用诺诺开放平台调发票接口，给用户开发票，并把发票发给用户邮箱*/

    @PostMapping("/native")
    public String nativeUpload(@RequestBody Map map) throws IOException, DocumentException {
        String username = (String) map.get("username");
        String mark = (String) map.get("mark");
        String mailbox = (String) map.get("mailbox");
        String money = (String) map.get("money");

        int num = (int)(Math.random()*100);
        //创建一个pdf文档
        Document document = new Document();
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\学习资料\\图片\\"+num+"票据.pdf"));
        //打开文档
        document.open();
        BaseFont bf =  BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //设置字体
        Font font = new Font(bf,20,Font.BOLD);	//字体颜色有三个参数，输出中文格式，字体大小，字体特点比如加粗
        //设置内容
        Paragraph paragraph = new Paragraph("票据",font);	//段落
        paragraph.setFirstLineIndent(14);		//缩进
        paragraph.setAlignment(1);		//1为居中对齐、2为右对齐、3为左对齐，默认为左对齐
        //设置内容
        Chunk temp = new Chunk("公司名称"+username+"\n",font);
        Chunk temp2 = new Chunk("税号"+mark+"\n",font);
        Chunk temp3 = new Chunk("邮箱"+mailbox+"\n",font);
        Chunk temp4 = new Chunk("金额"+money+"\n",font);
        /*   temp.setBackground(BaseColor.BLUE);	*/	//背景颜色
        temp.setTextRise(10);		//文本框大小
        //添加内容
        document.add(paragraph);
        document.add(temp);
        document.add(temp2);
        document.add(temp3);
        document.add(temp4);
        //把印章放进去
//        Image image = Image.getInstance("D:\\学习资料\\图片\\yz.png");
//        //设置图片的大小
//        image.scaleToFit(100f, 100f);			//图片大小	宽度，高度
//        image .setAlignment(Image.RIGHT);		//图片居中   Image.RIGHT、Image.MIDDLE、Image.LEFT分别指右对齐、居中、左对齐
//        document.add(image);
//        //获取刚才上传的签名的oss的路径,并同步到pdf合同中去
//        Image image2 = Image.getInstance("https://yjs-2004a.oss-cn-beijing.aliyuncs.com/"+username+".png");
//        image.scaleToFit(100f, 100f);			//图片大小	宽度，高度
//        image .setAlignment(Image.LEFT);		//图片居中   Image.RIGHT、Image.MIDDLE、Image.LEFT分别指右对齐、居中、左对齐
//        document.add(image2);

        //关闭文档
        document.close();
        //关闭书写器
        writer.close();

        //生产最终的pdf合同
        InputStream inputStream = new FileInputStream("D:\\学习资料\\图片\\"+num+"票据.pdf");
        //上传到oss中
       /* ossClient.putObject(bucketName,username+".pdf",inputStream);*/
        // 关闭OSSClient。
        ossClient.shutdown();
        System.out.println("上传成功");
        System.out.println(hostName+""+username+".pdf");
     /*   File file =new File("D:\\学习资料\\图片\\"+num+"票据.pdf");
        file.delete();*/
        SendMailUtil sendMailUtil = new SendMailUtil();

        sendMailUtil.setSubjectName("票据");

        sendMailUtil.setContent(hostName+""+username+".pdf");

       /* sendMailUtil.send();*/
   /*     File file =new File("D:\\学习资料\\图片\\"+num+"票据.pdf");
        file.delete();*/
        sc(num);

        return  hostName+""+username+".pdf";
    }

    @GetMapping("sc/{num}")
    public void sc(@PathVariable Integer num){
        File file =new File("D:\\学习资料\\图片\\"+num+"票据.pdf");
        file.delete();
    }
}
